﻿using System.Collections;
using Estsh.Core.Util;
using Estsh.Core.Models;
using Estsh.Core.Model.Result;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Dapper;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Wms.IServices;
using Estsh.Core.Services;

/***************************************************************************************************
 *
 * 更新人：sitong.dong
 * 描述：移动单管理
 * 修改时间：2022.06.22
 * 修改日志：系统迭代升级
 *
 **************************************************************************************************/
namespace Estsh.Core.Wms.Services
{
    public class MovementManageService : BaseService<WmsMoveHeader_Extend>, IMovementManageService
    {
        private readonly IMovementManageRepository repository;
        public MovementManageService(IMovementManageRepository _repository) : base(_repository)
        {
            repository = _repository;
        }
        public Hashtable getMoveHeaderListByPage(string orderNo, string orderType, string orderStatus, string startTime, string endTime, string enabled, int factoryId, Pager pager, String direction, String sort,string fedbatchType)
        {
            Hashtable result = new Hashtable();

            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.order_no like '%" + orderNo.Trim() + "%' ";
            }
            if (orderType != null && !orderType.Trim().Equals(""))
            {
                strWhere += "and a.order_type ='" + orderType.Trim() + "' ";
            }
            if (fedbatchType != null && !fedbatchType.Trim().Equals(""))
            {
                strWhere += "and a.FedbatchType ='" + fedbatchType.Trim() + "' ";
            }

            if (orderStatus != null && !orderStatus.Trim().Equals(""))
            {
                strWhere += "and a.order_status ='" + orderStatus.Trim() + "' ";
            }

            if (!String.IsNullOrEmpty(startTime) && !String.IsNullOrEmpty(endTime))
            {
                strWhere += " and  a.create_time BETWEEN '" + startTime.Trim() + "' AND '" + endTime.Trim() + "'";
            }
            else if (String.IsNullOrEmpty(startTime) && !String.IsNullOrEmpty(endTime))
            {
                strWhere += " and  a.create_time < '" + endTime.Trim() + "'";
            }
            else if (!String.IsNullOrEmpty(startTime) && String.IsNullOrEmpty(endTime))
            {
                strWhere += " and  a.create_time >'" + startTime.Trim() + "'";
            }

            if (enabled != null && !enabled.Trim().Equals(""))
            {
                strWhere += "and a.enabled = '" + enabled.Trim() + "' ";
            }
            strWhere += " and a.factory_id = " + factoryId + " ";
            String orderBy = "";
            if (sort != null && !"".Equals(sort.Trim()))
            {
                orderBy += typeof(WmsMoveHeader).GetEntityColumnName(sort.Trim()) + " " + direction;
            }
            else
            {
                orderBy += typeof(WmsMoveHeader).GetEntityColumnName("ruid") + " " + direction;
            }

            return repository.getMoveHeaderListByPage(pager.pageSize, pager.pageNo, strWhere, orderBy);

        }

        public Hashtable getMoveDetailListByPage(string orderNo, string enabled, int factoryId)
        {
            Hashtable result = new Hashtable();

            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.order_no like '%" + orderNo.Trim() + "%' ";
            }

            if (enabled != null && !enabled.Trim().Equals(""))
            {
                strWhere += "and a.enabled = '" + enabled.Trim() + "' ";
            }
            strWhere += " and a.factory_id = " + factoryId + " ";

            return repository.getMoveDetailListByPage(strWhere);

        }

        public Hashtable getMoveSnListByPage(string orderNo, string partNo, string enabled, int factoryId)
        {
            Hashtable result = new Hashtable();

            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.order_no = '" + orderNo.Trim() + "' ";
            }
            if (partNo != null && !partNo.Trim().Equals(""))
            {
                strWhere += "and a.part_no = '" + partNo.Trim() + "' ";
            }

            if (enabled != null && !enabled.Trim().Equals(""))
            {
                strWhere += "and a.enabled = '" + enabled.Trim() + "' ";
            }
            strWhere += " and a.factory_id = " + factoryId + " ";

            return repository.getMoveSnListByPage(strWhere);

        }
        public Hashtable onBarcodeGenerator(String ids)
        {
            ids = ids.Substring(0, ids.Length - 1);
            return this.repository.onBarcodeGenerator(ids);
        }
        public List<KeyValueResult> GetErpwarehouse()
        {
            return repository.GetErpwarehouse();
        }
        public List<KeyValueResult> GetMoveOrderType()
        {
            return repository.GetMoveOrderType();
        }
        public List<KeyValueResult> GetFedbatchType()
        {
            return repository.GetFedbatchType();
        }
        public List<KeyValueResult> GetFedbatchMethods()
        {
            return repository.GetFedbatchMethods();
        }
        public List<KeyValueResult> GetMoveOrderStatus()
        {
            return repository.GetMoveOrderStatus();
        }
        public List<KeyValueResult> getSelectFactory()
        {
            return repository.getSelectFactory();
        }

        public List<KeyValueResult> getSelectWarehouse()
        {
            return repository.getSelectWarehouse();
        }
        public List<SysWarehouse> getSelectWarehouse(string warehouseid)
        {
            return repository.getSelectWarehouse(warehouseid);
        }
        public List<SysZone> getSelectZone(string zoneid)
        {
            return repository.getSelectZone(zoneid);
        }
        public List<KeyValueResult> getSelectZone()
        {
            return repository.getSelectZone();
        }
        public List<SysLocate> getSelectLocate2(string locateid)
        {
            return repository.getSelectLocate2(locateid);
        }

        public List<KeyValueResult> getSelectVendor()
        {
            return repository.getSelectVendor();
        }

        public List<SysVendor> getSelectVendor(string vendorId)
        {
            return repository.getSelectVendor(vendorId);
        }
        public List<KeyValueResult> GetPart()
        {
            return repository.GetPart(0);
        }

        public List<SysPart> GetPart(string PartNo)
        {
            return repository.GetPart(0, PartNo);
        }
        /// <summary>
        /// 保存菜单数据
        /// </summary>
        /// <param name="htParams"></param>
        /// <returns></returns>
        public bool saveMovementManage(WmsMoveHeader_Extend htParams, IList<WmsMoveDetail_Extend> htDetailParams)
        {
            return repository.saveMovementManage(htParams, htDetailParams);
        }
        public List<SysPart> GetPartNoInfo(string part_no)
        {
            return this.repository.GetPartNoInfo(part_no);
        }
        public List<SysPart> GetPartNoInfoByPartNo(string part_no)
        {
            return this.repository.GetPartNoInfoByPartNo(part_no);
        }
        public List<SysPart> GetPartSpecInfo(string partSpec)
        {
            return this.repository.GetPartSpecInfo(partSpec);
        }
        public List<SysPart> GetPartSpecInfoByPartSpec(string partSpec)
        {
            return this.repository.GetPartSpecInfoByPartSpec(partSpec);
        }

        public List<MesWorkOrder> GetWorkRefOrderNo(string refOrderNo, string orderType)
        {
            return this.repository.GetWorkRefOrderNo(refOrderNo,orderType);
        }

        public List<MesWorkOrder> GetWorkOrderInfo(string refOrderNo)
        {
            return this.repository.GetWorkOrderInfo(refOrderNo);
        }

        public List<SysZone> getSelectZone2(string zoneid)
        {
            return repository.getSelectZone2(zoneid);
        }


        public List<KeyValueResult> getSelectLocate(int zoneId)
        {
            return repository.getSelectLocate(zoneId);
        }


        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool onClose(String ids, String empId)
        {
            ids = ids.Substring(0, ids.Length - 1);
            return this.repository.onClose(ids, empId);
        }

        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool EnableData(String ids)
        {
            ids = ids.Substring(0, ids.Length - 1);
            return this.repository.EnableData(ids);
        }

        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DisableData(String ids)
        {
            ids = ids.Substring(0, ids.Length - 1);
            return this.repository.DisableData(ids);
        }
    }
}